using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

namespace Common.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    public class ColorUtil
    {
        private static int ConvertFromHaxValues(string hexVal)
        {
            return int.Parse(hexVal, System.Globalization.NumberStyles.HexNumber);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hexColor"></param>
        /// <returns></returns>
        public static Color ConvertFromHexString(string hexColor)
        {
            if (hexColor.Length == 6)
            {
                int r = ColorUtil.ConvertFromHaxValues(hexColor.Substring(0, 2));
                int g = ColorUtil.ConvertFromHaxValues(hexColor.Substring(2, 2));
                int b = ColorUtil.ConvertFromHaxValues(hexColor.Substring(4, 2));
                return Color.FromArgb(r, g, b);
            }
            else
                return Color.FromKnownColor(KnownColor.Black);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Color> GetDistinctRandomColors(int count)
        {
            Random randomSeed = new Random();
            List<Color> colors = new List<Color>();
            for(int i=0;i<count;i++)
            {
                colors.Add(Color.FromArgb(
                               randomSeed.Next(256),
                               randomSeed.Next(256),
                               randomSeed.Next(256)));
            }
            return colors;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string ToString(Color color)
        {
            return string.Format("{0}:{1}:{2}:{3}", color.A, color.R, color.G, color.B);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="knownColor"></param>
        /// <returns></returns>
        public static string ToString(KnownColor knownColor)
        {
            Color color = Color.FromKnownColor(knownColor);
            return string.Format("{0}:{1}:{2}:{3}", color.A, color.R, color.G, color.B);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string ToString(System.Windows.Media.Color color)
        {
            return string.Format("{0}:{1}:{2}:{3}", color.A, color.R, color.G, color.B);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="colorStr"></param>
        /// <returns></returns>
        public static Color Parse(string colorStr)
        {
            string[] pieces = colorStr.Split(new char[] {':'});
            int a = int.Parse(pieces[0]);
            int r = int.Parse(pieces[1]);
            int g = int.Parse(pieces[2]);
            int b = int.Parse(pieces[3]);
            Color color = Color.FromArgb(a, r, g, b);
            return color;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="colorStr"></param>
        /// <returns></returns>
        public static System.Windows.Media.Color ParseMediaColor(string colorStr)
        {
            string[] pieces = colorStr.Split(new char[] { ':' });
            byte a = byte.Parse(pieces[0]);
            byte r = byte.Parse(pieces[1]);
            byte g = byte.Parse(pieces[2]);
            byte b = byte.Parse(pieces[3]);
            System.Windows.Media.Color color = System.Windows.Media.Color.FromArgb(a, r, g, b);
            return color;
        }
    }
}
